#ifndef MSFIFO_H
#define MSFIFO_H
#include <stdint.h>
#include <libmslog/mstype.h>
#include "msthread.h"
typedef struct MSFIFOBLOCKContext {
//块缓存的基本信息
	ms_u08 name[128];	//名称
	ms_u32 size_total;	//大小
	ms_pu08 ptr_begain;	//块开始位置
	ms_pu08 ptr_end;	//块结束位置
	MSLOCKContext mslock;//资源保护锁
//内部使用：用于管理数据存取的基本数据
	ms_pu08 ptr_in;		//指向数据存放入口
	ms_pu08 ptr_out;	//指向数据读取入口
	ms_u32 num_in;		//记录存放数据的数量
	ms_u32 num_out;		//记录读取数据的数量
//对外提供信息
	ms_u32 size_used;	//已使用空间块大小
	ms_u32 size_left;	//空闲空间块大小
	ms_u32 num_reset;	//异常复位次数
} MSFIFOBLOCKContext;

typedef struct MSFIFOLISTContext {
	ms_u32 total_num;
	ms_u32 len;
	ms_pvoid ptr;
//用于多个客户端的读取	
	ms_u64 inTime;	//数据进入时间
	struct MSFIFOLISTContext *next;
} MSFIFOLISTContext;

typedef struct MSFIFOSLISTContext {
	ms_u32 total_num;		//成员总数
	ms_s32 timeout_drop;	//成员超时丢弃时间，单位秒
	MSFIFOLISTContext *pHSList_ctt;
	MSFIFOLISTContext *pTSList_ctt;
	ms_u64 timebase_drop;	//定时器用
	ms_u64 inTime_last;			//最新数据进入时间
	MSLOCKContext mslock;//资源保护锁
}MSFIFOSLISTContext;
typedef struct MSBFIFOSIZEContext{
	ms_u32 fifosize_left;
	ms_u32 fifosize_total;
	ms_u32 num_reset;			//错误复位次数
	ms_u32 num_flowoverreset;	//数据溢出复位次数
	MSFIFOBLOCKContext * pBFifo_ctt;
}MSBFIFOSIZEContext;

#ifndef MSFIFO_C
#define INTERFACE_MSFIFO extern
#else
#define INTERFACE_MSFIFO
#endif
//FIFO BLOCK
INTERFACE_MSFIFO MSFIFOBLOCKContext *
	msfifo_api_blockInit(ms_u32 ms_in size,ms_string ms_in name);
INTERFACE_MSFIFO ms_void 
	msfifo_api_blockDeinit(MSFIFOBLOCKContext **  ms_io ppmsfifoblock_ctt);
/*|--data(len byte)--|*/
INTERFACE_MSFIFO ms_s32 
	msfifo_api_blockIn(MSFIFOBLOCKContext * ms_io pmsfifoblock_ctt, ms_pvoid ms_in pbuf, ms_u32 ms_in size,ms_s32  ms_in (*func)(ms_pvoid, ms_pvoid, ms_u32));
INTERFACE_MSFIFO ms_s32 
	msfifo_api_blockOut(MSFIFOBLOCKContext * ms_io pmsfifoblock_ctt, ms_pvoid ms_in pbuf, ms_u32 ms_in size,ms_s32  ms_in (*func)(ms_pvoid, ms_pvoid, ms_u32));
/*|--len(4 byte)--|--data(len byte)--|*/
INTERFACE_MSFIFO ms_s32 
	msfifo_api_blockLenIn(MSFIFOBLOCKContext * ms_io pmsfifoblock_ctt, ms_pvoid ms_in pbuf, ms_s32 ms_in size);
INTERFACE_MSFIFO ms_s32 
	msfifo_api_blockLenOut1(MSFIFOBLOCKContext * ms_io pmsfifoblock_ctt, ms_pvoid ms_in pbuf, ms_u32 ms_in size,ms_cstring ms_in func, ms_u32 ms_in line);
INTERFACE_MSFIFO ms_u32 
	msfifo_api_blockSizeUsed(MSFIFOBLOCKContext * ms_in pmsfifoblock_ctt);
INTERFACE_MSFIFO  ms_u32 
	msfifo_api_blockSizeLeft(MSFIFOBLOCKContext * ms_in pmsfifoblock_ctt);
INTERFACE_MSFIFO  ms_u32 
	msfifo_api_blockResetNum(MSFIFOBLOCKContext * ms_in pmsfifoblock_ctt);
INTERFACE_MSFIFO ms_u32 
	msfifo_api_blockReset(MSFIFOBLOCKContext * ms_io pmsfifoblock_ctt);
INTERFACE_MSFIFO ms_void 
	msfifo_api_blockReset2(MSBFIFOSIZEContext * ms_io pmsfifsize_ctt);

//FIFO LIST
INTERFACE_MSFIFO MSFIFOLISTContext *
	msfifo_api_listMalloc(ms_u32 ms_in size,ms_string name);
INTERFACE_MSFIFO ms_void 
	msfifo_api_listDemalloc(MSFIFOLISTContext ** ms_io ppmsfifolist_ctt);
INTERFACE_MSFIFO ms_void 
	msfifo_api_listIn(MSFIFOLISTContext ** ms_io ppheader_fifolist_ctt,MSFIFOLISTContext * ms_in pmsfifolist_ctt);
INTERFACE_MSFIFO MSFIFOLISTContext *
	msfifo_api_listOut(MSFIFOLISTContext ** ms_in ppheader_fifolist_ctt);
INTERFACE_MSFIFO ms_u32 
	msfifo_api_listTotalNum(MSFIFOLISTContext ** ms_in ppheader_fifolist_ctt);
INTERFACE_MSFIFO ms_void 
	msfifo_api_listReset(MSFIFOLISTContext ** ms_in ppheader_fifolist_ctt);
INTERFACE_MSFIFO ms_void 
	msfifo_api_shareListInit(MSFIFOSLISTContext *pSFifo_ctt,ms_string name);
INTERFACE_MSFIFO ms_void 
	msfifo_api_shareListDeinit(MSFIFOSLISTContext *pSFifo_ctt);
INTERFACE_MSFIFO MSFIFOLISTContext *
	msfifo_api_shareListMalloc(ms_u32 ms_in size,ms_string name);
INTERFACE_MSFIFO ms_void 
	msfifo_api_shareListDemalloc(MSFIFOLISTContext ** ms_io ppIdList_ctt);
INTERFACE_MSFIFO ms_void 
	msfifo_api_shareListIn(MSFIFOSLISTContext *pSFifo_ctt,MSFIFOLISTContext * ms_in pIdList_ctt_in);
INTERFACE_MSFIFO 
	MSFIFOLISTContext *msfifo_api_shareListRead(MSFIFOSLISTContext *pSFifo_ctt,ms_u64 lastReadTime);
INTERFACE_MSFIFO ms_u32 
	msfifo_api_shareListTotalNum(MSFIFOSLISTContext *pSFifo_ctt);
INTERFACE_MSFIFO 
	ms_void msfifo_api_shareListReset(MSFIFOSLISTContext *pSFifo_ctt);

#define msfifo_blockLenOut(pmsfifoblock_ctt, pbuf,size)	msfifo_api_blockLenOut1(pmsfifoblock_ctt, pbuf, size,__FUNCTION__,__LINE__);
#define msfifo_blockLenOut_2048(pmsfifoblock_ctt, pbuf)	msfifo_blockLenOut(pmsfifoblock_ctt, pbuf, 2048);

#endif /* FIFO_H */

